<?php

/**
 * contact : 刘经理 19326416371
 * company : 版权所属 四川北星惯牌科技有限公司
 **/

namespace app\api\controller;

use app\common\controller\Api;
use app\common\manager\BaseManager;
use app\common\manager\SignManager;
use think\Config;
use think\Exception;

/**
 * 名片管理
 */
class Sign extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = '*';

    public function _initialize()
    {
        parent::_initialize();

        if (!Config::get('fastadmin.usercenter')) {
            $this->error(__('User center already closed'));
        }

    }

    /**
     * 名片添加
     * @param $name 姓名
     * @param $mobile 手机号
     * @param $gender 性别 1=男,2=女
     * @param $default 默认 1=是,0=否
     * @return void
     */
    public function create(){
        $user_id = $this->user_id;
        try {

            $real_name = $this->request->post('real_name');
            $mobile = $this->request->post('mobile');
            $captcha = $this->request->post('captcha');
            $gender = $this->request->post('gender') ?? 1;
            $default = $this->request->post('default') ?? 0;
            $data = [
                'real_name' => $real_name,
                'mobile' => $mobile,
                'captcha' => $captcha,
                'gender' => $gender,
                'default' => $default
            ];

            $signValidate = new \app\api\validate\Sign();
            if(!$signValidate->check($data)){
                throw new Exception($signValidate->getError());
            }
            unset($data['captcha']);
            $result = SignManager::createSign($user_id,$data);
            if ($result['code'] == BaseManager::$successCode) {
                $this->success($result['message'],$result['data']);
            }else{
                throw new Exception($result['message']);
            }
        }catch (Exception $exception){
            $this->error($exception->getMessage());
        }

    }

    /**
     * 修改名片信息
     * @return void
     */
    public function modify()
    {
        $user_id = $this->user_id;
        try {
            $id = $this->request->post('id');
            $real_name = $this->request->post('real_name');
            $gender = $this->request->post('gender') ?? 1;
            $default = $this->request->post('default') ?? 0;

            $data = [
                'real_name' => $real_name,
                'gender' => $gender,
                'default' => $default
            ];

            $signValidate = new \app\api\validate\SignUpdate();
            if($signValidate->check($data)){
                throw new Exception($signValidate->getError());
            }
            $result = SignManager::updateSign($user_id,$id,$data);
            if ($result['code'] == 1) {
                $this->success($result['message']);
            }else{
                throw new Exception($result['message']);
            }
        }catch (Exception $exception){
            $this->error($exception->getMessage());
        }
    }

    public function drop()
    {
        $user_id = $this->user_id;
        try {
            $id = $this->request->post('id');
            $result = SignManager::deleteSign($user_id,$id);
            if ($result['code'] == 1) {
                $this->success($result['message']);
            }else{
                throw new Exception($result['message']);
            }
        }catch (Exception $exception){
            $this->error($exception->getMessage());
        }
    }

    public function list()
    {
        $user_id = $this->user_id;
        try {
            $page = $this->request->post('page') ?? 1;
            $result = SignManager::getSign($user_id,$page);
            if ($result['code'] == 1) {
                $this->success($result['message'],$result['data']);
            }else{
                throw new Exception($result['message']);
            }
        }catch (Exception $exception){
            $this->error($exception->getMessage());
        }
    }
}
